<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <comment-item
      @click-reply="$emit('click-reply', $event)"
      :comment="item"
      v-for="item in list"
      :key="item.com_id + ''"
    ></comment-item>
  </van-list>
</template>

<script>
import { getCommentList } from '@/api/article.js'
import CommentItem from '@/components/CommentItem.vue'
export default {
  components: { CommentItem },
  data () {
    return {
      loading: false,
      finished: false,
      limit: 10,
      offset: null
    }
  },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    list: {
      type: Array,
      default: () => {}
    },
    source: {
      type: [Object, Number, String],
      required: true
    }
  },
  created () {},

  methods: {
    async onLoad () {
      try {
        const {
          results,
          last_id: lastId,
          total_count: total
        } = await getCommentList({
          type: this.type,
          source: this.source,
          limit: this.limit,
          offset: this.offset
        })

        this.$emit('on-success', total)
        this.list.push(...results)
        this.loading = false
        if (!results.length) {
          this.finished = true
        } else {
          this.offset = lastId
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
